package com.example.lenovo.weiyi;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.com.ruanmeng.demon.CommonM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

public class ChangeBMActivity extends BaseActivity {

    @BindView(R.id.bt_bm_queding)
    Button btQueding;
    @BindView(R.id.tv_change_fbm)
    EditText tvChangeFbm;
    @BindView(R.id.tv_change_sbmi)
    EditText tvChangeSbmi;
    @BindView(R.id.tv_change_tbm)
    EditText tvChangeTbm;
    @BindView(R.id.iv_bm_first)
    ImageView ivBmFirst;
    @BindView(R.id.iv_bm_s)
    ImageView ivBmS;
    @BindView(R.id.iv_bm_t)
    ImageView ivBmT;
    private String bm_str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_bm);
        ButterKnife.bind(this);
        ChangLayTitle("部门");
        LayBack();
        init();
    }

    public void init() {
        btQueding.setClickable(false);
        tvChangeFbm.addTextChangedListener(this);
        tvChangeSbmi.addTextChangedListener(this);
        tvChangeTbm.addTextChangedListener(this);
        String bm_string = getIntent().getStringExtra("bm");
        String[] bmstr = bm_string.split("-");
        if (bmstr.length >= 1) {
            tvChangeFbm.setText(bmstr[0]);
        }
        if (bmstr.length >= 2) {
            tvChangeSbmi.setText(bmstr[1]);
        }
        if (bmstr.length == 3) {
            tvChangeTbm.setText(bmstr[2]);
        }


    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        super.onTextChanged(charSequence, i, i1, i2);
        if (CommonUtil.getEditText(tvChangeFbm).isEmpty()) {
            btQueding.setBackgroundResource(R.drawable.ed_fullgeray);
            btQueding.setClickable(false);
            ivBmFirst.setVisibility(View.GONE);
        } else {
            btQueding.setBackgroundResource(R.drawable.ed_fullmain);
            btQueding.setClickable(true);
            ivBmFirst.setVisibility(View.VISIBLE);
        }
        if (CommonUtil.getEditText(tvChangeSbmi).isEmpty()) {
            ivBmS.setVisibility(View.GONE);
        } else {
            ivBmS.setVisibility(View.VISIBLE);
        }
        if (CommonUtil.getEditText(tvChangeTbm).isEmpty()) {
            ivBmT.setVisibility(View.GONE);
        } else {
            ivBmT.setVisibility(View.VISIBLE);
        }
    }

    public void doClick(View v) {
        switch (v.getId()) {
            case R.id.bt_bm_queding://queding
                if (TextUtils.isEmpty(tvChangeFbm.getText().toString())) {
                    CommonUtil.showToask(ChangeBMActivity.this, "请输入一级部门！");
                    return;
                }
                //部门
                StringBuffer buffer = new StringBuffer("");
                buffer.append(tvChangeFbm.getText().toString());
                if (!TextUtils.isEmpty(tvChangeSbmi.getText().toString())) {
                    buffer.append("-");
                    buffer.append(tvChangeSbmi.getText().toString());
                }
                if (!TextUtils.isEmpty(tvChangeTbm.getText().toString())) {
                    buffer.append("-");
                    buffer.append(tvChangeTbm.getText().toString());
                }
                bm_str = buffer.toString();
                getData();
                break;
            case R.id.iv_bm_first:
                tvChangeFbm.setText("");
                break;
            case R.id.iv_bm_s:
                tvChangeSbmi.setText("");
                break;
            case R.id.iv_bm_t:
                tvChangeTbm.setText("");
                break;
        }
    }

    /**
     * 修改个人资料
     */

    private void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.UpdatePerMess, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ChangeBMActivity.this, "uid"));
        mRequest.add("deptName", bm_str);//部门
        CallServer.getRequestInstance().add(ChangeBMActivity.this, 0, mRequest,
                new CustomHttpListener(ChangeBMActivity.this, true, CommonM.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CommonM model = ((CommonM) data);
                        PreferencesUtils.putString(ChangeBMActivity.this, "bumen", bm_str);
                        finish();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if (!("100").equals(obj.getString("msgcode"))) {
                            CommonUtil.showToask(ChangeBMActivity.this, obj.getString("msg"));
                        }
                    }
                }, true, true);
    }

}
